SQL::TableSnapshot Example: Scripting the Load

Description

You can create a snapshot yourself, programmatically; as in this example.

SQL database snapshot objects load and store data and definitions with the help of other objects available to the Xbasic programmer. You can create a snapshot yourself, programmatically; as in this example. Here, the TableInfo for the table named Contacts is retrieved so that the TableSnapshot has a table definition (we could have scripted the creation of the TableInfo, but that is outside of the scope of this article). Two instances of RowSnapshot are created and populated with data and then added to the TableSnapshot.

dim cn as SQL::Connection
dim ti as SQL::TableInfo
 
dim ts as SQL::TableSnapshot
dim Row as SQL::RowSnapshot
dim Col as SQL::ColumnValue
 
' Open the source database
cn.open("::Name::MyDatabase")

' Load the schema from the source database
cn.GetTableInfo(ti, "Contact")
 
ts.TableInfo = ti
 
' Add two rows 
 
''' Row 1 - Fred Smith '''

' Create a new row
Row = Row.NewInstance()
 
' Create a new column
Col = col.NewInstance() 
Col.Name = "LName"
Col.Value = "Smith"

' Add the column to the row
Row.AddData(Col)
 
' Create a new Column
Col = Col.NewInstance()			
Col.Name = "FName"
Col.Value = "Fred"

' Add the column to the row
Row.AddData(Col)
 
' Add the row to the table snapshot
ts.AddRow(Row)
 
''' Row 2 - Bob Jones '''

' Create a new row
Row = Row.NewInstance()

' Create a new column 
Col = col.NewInstance()
Col.Name = "LName"
Col.Value = "Jones"

' Add the column to the row
Row.AddData(Col)

' Create a new Column
Col = Col.NewInstance()
Col.Name = "FName"
Col.Value = "Bob"

' Add the column to the row
Row.AddData(Col)

' Add the row to the table snapshot
ts.AddRow(Row)

''' Persist the snapshot to a file. '''

' Save the snapshot as XML
file.from_string("<MyFile>", ts.XML)